package com.acme.bankapp.domain.bank;

import java.util.ArrayList;
import java.util.List;

import com.acme.bankapp.service.bank.ClientRegistrationListener;
import com.acme.exceptions.ClientNotExistException;


public class Bank {
	private List<Client> clients = new ArrayList<Client>();	
	private List<ClientRegistrationListener> listeners = new ArrayList<ClientRegistrationListener>();
	private ClientRegistrationListener debugListener = new DebugListener();
	public Bank() {
		listeners.add(new ClientRegistrationListener() {
			
			@Override
			public void onClientAdded(Client c) {
				System.out.println("Client: " + c.getClientSalutation() + ", welcomoe!");
			}
		});
		listeners.add(new ClientRegistrationListener() {
			
			@Override
			public void onClientAdded(Client c) {
				System.out.println("Notification email for client " + c.getClientSalutation() + " to be sent");
			}
		});
		listeners.add(debugListener);
	}
	
	public class DebugListener implements ClientRegistrationListener{

		@Override
		public void onClientAdded(Client c) {
			System.out.println("Client " + c.getClientSalutation() +" added on " + new java.util.Date());
		}
		
	}
	
	public void addClient(Client client) {
		clients.add(client);
		for(ClientRegistrationListener l : this.listeners) {
			l.onClientAdded(client);
		}
	}
	
	public Client getClientById(int id) throws ClientNotExistException {
		if(id >= 0) {
			if(clients.size() > id) {
				return clients.get(id);
			} else {
				throw new ClientNotExistException("Client do not exist!");
			}	
		} else {
			throw new IllegalArgumentException("id incorrect");
		}
	}
	
	public int getClientCount() {
		return this.clients.size();
	}
}
